package com.hyh.blog.controller.admin;

import com.hyh.blog.po.Type;
import com.hyh.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jws.WebParam;

/**
 * @author Summerday
 */

@Controller
@RequestMapping("/admin/types")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @GetMapping()
    public String types(@PageableDefault(size = 10, sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable,
                        Model model){

        model.addAttribute("page",typeService.listType(pageable));
        return "admin/types";
    }

    @PostMapping()
    public String addTypes(Type type){
        Type t = typeService.saveType(type);
        if(t == null){

        }else{

        }
        return "redirect:/admin/types";
    }

    @GetMapping("/{id}/delete")
    public String delete(@PathVariable("id") Long id){
        typeService.deleteType(id);
        return "redirect:/admin/types";
    }

    @GetMapping("/{id}/edit")
    public String get(@PathVariable("id") Long id,Model model){
        Type type = typeService.getType(id);
        model.addAttribute("type",type);
        return "redirect:/admin/types";
    }

}
